package cn.qvtu.game.frame;

import cn.qvtu.game.App;
import cn.qvtu.game.model.BackGround;
import cn.qvtu.game.model.Enemy;
import cn.qvtu.game.model.Person;
import cn.qvtu.game.model.Tools;
import cn.qvtu.game.util.NumberUtils;
import cn.qvtu.game.util.StaticValue;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.List;

import static java.awt.event.KeyEvent.VK_ESCAPE;

/**
 * 游戏窗体
 */
public class MyFrame extends JFrame implements Runnable, KeyListener
{
    //游戏的开始时间
    private Date startDate = new Date();
    //添加人物
    private Person person,person1;
    //当前的场景
    private BackGround nowBackGround;
    private Tools tools;
    public int count = 0;
    /**
     * 键入某个键时调用此方法
     */
    public void keyTyped(KeyEvent e)
    {

    }
    /**
     * 按下某个键时调用此方法
     */
    public void keyPressed(KeyEvent e)
    {
        int keyCode = e.getKeyCode();
        if(keyCode == 68)
        {
            //按下D键执行右跑
            this.person.RightRun();
        }
        if(keyCode == 65)
        {
            //按下A键执行左跑
            this.person.LeftRun();
        }
        if (keyCode == 74)
        {
            //按下J键攻击
            this.person.attack();
        }

        if(keyCode == 75)
        {

            while (count != 2)
            {
                //按下K键跳跃
                this.person.Jump();
                count++;
            }
        }

        if (keyCode == KeyEvent.VK_RIGHT)
        {
            //按下➡键执行右跑
            this.person1.RightRun();
        }

        if (keyCode == KeyEvent.VK_LEFT)
        {
            //按下⬅键执行左跑
            this.person1.LeftRun();
        }

        if (keyCode == VK_ESCAPE)
        {
            //按下ESC键退出
            new App();
        }

        if (keyCode == KeyEvent.VK_1)
        {
            //按下1键攻击
            this.person1.attack();
        }

        if (keyCode == KeyEvent.VK_2)
        {
            //按下2键跳跃
            this.person1.Jump();
        }

        if(keyCode == KeyEvent.VK_F)
        {
            //按下F键捡起道具
            this.person.takeTools();
        }
    }
    /**
     * 释放某个键时调用此方法
     */
    public void keyReleased(KeyEvent e)
    {
        int keyCode = e.getKeyCode();
        if(keyCode == 68)
        {
            //释放D键停止右跑
            this.person.StopRightRun();
        }
        if(keyCode == 65)
        {
            //释放A键停止左跑
            this.person.StopLeftRun();
        }
        if (keyCode == 75)
        {
            count--;
        }
        if (keyCode == KeyEvent.VK_RIGHT)
        {
            //释放➡停止右跑
            this.person1.StopRightRun();
        }

        if (keyCode == KeyEvent.VK_LEFT)
        {
            //释放⬅键停止左跑
            this.person1.StopLeftRun();
        }

    }
    public MyFrame()
    {
        //窗体标题
        this.setTitle("game");
        //窗体大小
        this.setSize(900,600);
        //设置窗体居中
        int width= Toolkit.getDefaultToolkit().getScreenSize().width;
        int height=Toolkit.getDefaultToolkit().getScreenSize().height;
        this.setLocation((width-900)/2,(height-600)/2);
        //初始化场景
        nowBackGround = new BackGround(1);
        nowBackGround.setPerson(person);
        nowBackGround.setTools(tools);
        //设置关闭按钮
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置可见
        this.setVisible(true);
        //监听键盘
        this.addKeyListener(this);
        //创建人物
        person = new Person(0,305,nowBackGround);//初始化显示的坐标(x=0,y=305)
        person1 =new Person(0,305,nowBackGround);
        tools= new Tools(0,235,nowBackGround);
        nowBackGround.setPerson(person);
        //创建线程对象，参数为实现了Runnable接口的任务对象
        Thread thread = new Thread(this);
        //线程启动
        thread.start();
    }

    //重写父类paint方法，渲染窗体的背景
    //当使用JFrame创建出窗体时，窗体的尺寸改变或者窗体的部分像素被移动到屏幕之外，都会导致窗体的刷新
    public void paint(Graphics g)
    {
        //创建图片对象，宽度900，高度600跟窗体一致
        BufferedImage image = new BufferedImage(900,600,BufferedImage.TYPE_3BYTE_BGR);
        //获取图像的笔画
        Graphics graphics = image.getGraphics();
        if(nowBackGround.getHp()<=0)
        {
            graphics.drawImage(StaticValue.defeat, 0, 0, 900,600,this);
            g.drawImage(image, 0, 0, this);
            return;
        }
        else if(nowBackGround.getHp()>0 && person.getScore() == 1100)
        {
            graphics.drawImage(StaticValue.victory, 0, 0, 900,600,this);
            g.drawImage(image, 0, 0, this);
            return;
        }
        //绘画图像，添加图像对象bg01,从坐标(0,0)开始绘画
        graphics.drawImage(nowBackGround.getShowBgImage(),0,0,this);
        //右上角添加游戏时间
        long dif = new Date().getTime()-startDate.getTime();
        //绘画时间，第一个参数渲染的文本，第2,3个参数渲染的坐标
        graphics.drawString("时间"+(dif/1000/60)+" : "+(dif/1000%60),800,60);
        //画敌人
        List<Enemy> allEnemy = nowBackGround.getAllEnemy();
        for (Enemy enemy:allEnemy)
        {
            graphics.drawImage(enemy.getShowImage(),enemy.getX(),enemy.getY(),this);
        }
        //画人物
        graphics.drawImage(person.getShowImage(),person.getX(),person.getY(),this);
        //画血条
        graphics.drawString("血量："+nowBackGround.getHp(),this.getWidth()-100,80);
        graphics.setColor(Color.green);
        if(nowBackGround.getHp()/10000.0<0.3) {
            graphics.setColor(Color.red);
        }
        graphics.drawRect(person.getX()+80, person.getY()+20, 100, 10);
        graphics.fillRect(person.getX()+80, person.getY()+20, (int)((100*(nowBackGround.getHp()))/10000.0), 10);
        //画得分
        if(person.getScore()!=0)
        {
            graphics.drawImage(NumberUtils.getNumberImage(person.getScore()+"",this),400,60,this);
        }
        //画道具
        List<Tools>allTools=nowBackGround.getAllTools();
        for(Tools tools:allTools)
        {
            graphics.drawImage(tools.getShowImage(),tools.getX(),tools.getY(),this);
        }
        //窗体的画笔绘画image图像
        g.drawImage(image,0,0,this);
        //是否进入下一场景
        if (nowBackGround.isPass(person) && nowBackGround.hasNext())
        {
            nowBackGround = nowBackGround.next();//获取下一个场景
            person.setX(0);//恢复人物初始位置
            person.setBackGround(nowBackGround);//重新设置场景
        }
    }
    public Person getPerson()
    {
        return person;
    }
    public void setPerson(Person person)
    {
        this.person = person;
    }
    /**
     * 线程运行的任务
     */
    public void run()
    {
        while(true)
        {
            try
            {
                //循环重新绘画窗口
                this.repaint();
                //线程休眠50毫秒
                Thread.sleep(50);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
}
